<?php
// classe de base, avec ses membres et ses méthodes
class Wikimgr extends Controller{

   function Wikimgr() {
      parent::Controller();
      $this->load->model('wiki_model');
   }

    function index()
    {
    	 redirect("/wiki/read/");
    }
   
   /**
    * Edits the current page, if it's new : creates an empty one
    * @param  string  $pagename  page to edit
    */
    function edit($pagename="")
    {
        //are we creating a new page or editing an existing one?
        $basepagename=$this->wiki_model->basePagename($pagename);
        if ($this->wiki_model->pageExists($pagename)) {
          if (!$this->user->isAuthorized()) {
                if (!$this->user->isLogged()) {
                    $this->session->set_flashdata('redir','/admin/wikimgr/edit/'.$pagename);
                    redirect("/admin/login");
                } else {
                    //logged but not authorized
                }
              }
            $raw=read_file($this->wiki_model->getPageFilename($pagename));
            $firstline = substr($raw,0,strpos($raw,"\n"));
            $content = substr($raw,strpos($raw,"\n"));
            $meta = @unserialize($firstline);
            if ($meta!==false) {
                $raw=$content;
            }
          $output['content']=$raw;
        } else {
           //creating a new page -> let's filter the pagename
           if ($pagename!=$basepagename)
               redirect ("/admin/wikimgr/edit/".$basepagename);
           if (!$this->user->isAuthorized("/wiki/create/".$pagename))
              redirect("/wiki/read/".$pagename);
           $output['content']="New page";
        }
         $this->load->helper('wiki');
        
        $output['pagename']  = $pagename;
        $output['basepagename']  = $basepagename;
        $output['versions']  = $this->wiki_model->getVersions($basepagename);
        $output['templates'] = $this->wiki_model->getStyles();
        $output['isLogged']  = $this->user->isLogged();

        $this->load->view('/admin/wikimgr/edit.html.php',$output);
    }

   /**
    * Saves the current page
    * @param  string  $pagename  page to save
    */
    function save($pagename)
    {
        if (!$this->user->isAuthorized())
            redirect("/wiki/read/".$pagename);
        $this->wiki_model->savePage($pagename,stripslashes($this->input->post('content')));
        $this->wiki_model->writeMeta($pagename,$this->input->post('meta'));
        redirect("/wiki/read/".$pagename);
    }
    
   /**
    * Deletes the current page -> moves data files in the trash dir
    * @param  string  $pagename  page to delete
    */
    function delete($pagename)
    {
        if (!$this->user->isAuthorized())
            redirect("/wiki/read/".$pagename);
        $this->wiki_model->deletePage($pagename);
        redirect("/wiki/read/");
    }
    
    function createlink()
    {
        $output['files']=array("file1","file2");
        $this->load->view('/admin/wikimgr/createlink.html.php',$output);
    }
  
}

?>
